package smartbooks.ui.options;

import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import static smartbooks.ui.util.CommonConstants.*;

/**
 *
 * @author Ankit
 */
public class OptionsPanel extends javax.swing.JPanel {
    TreePath currentSelectedOptionPath = null;
    
    // All the types of options panel are initialised
    GeneralOptionPanel generalOptionPanel = new GeneralOptionPanel();
    InvoiceLayoutOptionPanel invoiceLayoutOptionPanel = new InvoiceLayoutOptionPanel();
    InvoiceLayout_ContentOptionPanel invoiceLayout_ContentOptionPanel = new InvoiceLayout_ContentOptionPanel();
    InvoiceLayout_StyleOptionPanel invoiceLayout_StyleOptionPanel = new InvoiceLayout_StyleOptionPanel();
    InvoiceLayout_PackingSlipOptionPanel invoiceLayout_PackingSlipOptionPanel = new InvoiceLayout_PackingSlipOptionPanel();
    TaxOptionPanel taxOptionPanel = new TaxOptionPanel();
    RegionallOptionPanel regionallOptionPanel = new RegionallOptionPanel();
    OtherOptionPanel otherOptionPanel = new OtherOptionPanel();
    
    /**
     * Creates new form OptionsPanel
     */
    public OptionsPanel() {
        initComponents();
        
        customInitComponents();
    }
    
    private void customInitComponents() {
        optionsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        optionsTree.setSelectionRow(1); // Setting to General options at the start
        changeOptionPanel();
    }
    
    private void changeOptionPanel() {
        TreePath selectedPath = optionsTree.getSelectionPath();
        String selectedPathString = selectedPath.getLastPathComponent().toString();
        
        if(selectedPathString.equals(SMARTBOOKS_OPTION)) {
            optionsTree.setSelectionPath(currentSelectedOptionPath);
        } else if(selectedPath!=currentSelectedOptionPath) {
            currentSelectedOptionPath = selectedPath;
            String currentSelectedOptionPathString = currentSelectedOptionPath.getLastPathComponent().toString();
            
            JPanel panel = new JPanel();
            selectedOptionPanel.removeAll();
            
            switch (currentSelectedOptionPathString) {
                case GENERAL_OPTION:
                    panel = generalOptionPanel;
                    break;
                case INVOICE_LAYOUT_OPTION:
                    panel = invoiceLayoutOptionPanel;
                    break;
                case INVOICE_LAYOUT_CONTENT_OPTION:
                    panel = invoiceLayout_ContentOptionPanel;
                    break;
                case INVOICE_LAYOUT_STYLE_OPTION:
                    panel = invoiceLayout_StyleOptionPanel;
                    break;
                case INVOICE_LAYOUT_PACKINGS_SLIP_OPTION:
                    panel = invoiceLayout_PackingSlipOptionPanel;
                    break;
                case TAX_OPTION:
                    panel = taxOptionPanel;
                    break;
                case REGIONAL_OPTION:
                    panel = regionallOptionPanel;
                    break;
                case OTHER_OPTION:
                    panel = otherOptionPanel;
                    break;
            }
            
            selectedOptionPanel.add(panel);
            Dimension dim = new Dimension((int)panel.getPreferredSize().getWidth()+235, (int)panel.getPreferredSize().getHeight()+112);
            this.setMinimumSize(dim);
            panel.setSize(panel.getPreferredSize());
            panel.setVisible(true);

            selectedOptionPanel.revalidate();
            selectedOptionPanel.repaint();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        optionsTree = new javax.swing.JTree();
        selectedOptionPanel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        optionsTree.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("SmartBooks");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("General");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Invoice Layout");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Content");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Style");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Packing Slip");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Tax");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Regional");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Other");
        treeNode1.add(treeNode2);
        optionsTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        optionsTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                optionsTreeMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(optionsTree);

        javax.swing.GroupLayout selectedOptionPanelLayout = new javax.swing.GroupLayout(selectedOptionPanel);
        selectedOptionPanel.setLayout(selectedOptionPanelLayout);
        selectedOptionPanelLayout.setHorizontalGroup(
            selectedOptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 487, Short.MAX_VALUE)
        );
        selectedOptionPanelLayout.setVerticalGroup(
            selectedOptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setText("OK");

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton2.setText("Cancel");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(selectedOptionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .addComponent(selectedOptionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void optionsTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_optionsTreeMouseClicked
        changeOptionPanel();
    }//GEN-LAST:event_optionsTreeMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTree optionsTree;
    private javax.swing.JPanel selectedOptionPanel;
    // End of variables declaration//GEN-END:variables
}
